﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.service
{
    public class CADService
    {
        /// <summary>
        /// 取得所有文件夹路径
        /// </summary>
        /// <returns></returns>
        public static DataTable GetFilesIndex()
        {
            String sql = "SELECT\n" +
"	t_cad_index.ID AS ID,\n" +
"	t_cad_index.PID AS PID,\n" +
"	t_cad_index.FILE_ID AS FILE_ID,\n" +
"	t_cad_index.NAME AS NAME,\n" +
"	t_cad_index.FILE_FLAG AS FILE_FLAG \n" +
"FROM\n" +
"	t_cad_index";
            return SQLmtm.GetDataTable(sql);
        }
        /// <summary>
        /// 查询目录下的所有文件
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public static DataTable GetFilesByPID(String PID)
        {
            String sql = "SELECT\n" +
                "	t_cad_files.ID AS ID,\n" +
                "	t_cad_files.CAD_FILE_NAME AS CAD_FILE_NAME,-- t_cad_files.CAD_FILE AS CAD_FILE,\n" +
                "	t_cad_files.MD5 AS MD5,\n" +
                "	( CASE ENABLE_FLAG WHEN 0 THEN '否' ELSE '是' END ) AS ENABLE_FLAG,-- t_cad_files.ENABLE_FLAG AS ENABLE_FLAG,\n" +
                "	t_cad_files.UPDATE_USER AS UPDATE_USER,\n" +
                "	t_cad_files.UPDATE_DATE AS UPDATE_DATE, \n" +
                "	t_cad_files.STYLE_FIT_CD AS STYLE_FIT_CD, \n" +
                "	( CASE SAMPLE_FLAG WHEN 0 THEN '否' ELSE '是' END ) AS SAMPLE_FLAG\n" +
                "FROM\n" +
                "	t_cad_index\n" +
                "	LEFT JOIN t_cad_files ON t_cad_index.FILE_ID = t_cad_files.ID \n" +
                "WHERE\n" +
                "	t_cad_index.PID = '"+PID+"' \n" +
                "	AND FILE_FLAG = '1'";
            return SQLmtm.GetDataTable(sql);
        }
        /// <summary>
        /// 更新CAD文件
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Boolean UpdataCADFile(String ID, String path, String fileName)
        {
            try
            {
                String file = FileBinaryConvertHelper.PathToString(path);
                SQLmtm.DoUpdate("t_cad_files", new string[] { "CAD_FILE_NAME", "CAD_FILE", "MD5", "UPDATE_USER" }, new string[] { fileName, file, "", "" }, new string[] { "ID" }, new string[] { ID });
                SQLmtm.DoUpdate("t_cad_index", new string[] { "NAME" }, new string[] { fileName }, new string[] { "FILE_ID" }, new string[] { ID });
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 创建文件或目录
        /// </summary>
        /// <param name="path">文件位置</param>
        /// <param name="fileName">名称</param>
        /// <param name="pid">父id</param>
        /// <param name="fileFlag">是否是文件</param>
        /// <returns></returns>
        public static Boolean CreateFile(String path,String fileName,String pid,String fileFlag,String STYLE_FIT_CD)
        {
            try
            {
                String uuid = "";
                if(fileFlag=="1")
                {
                    uuid = new FunctionHelper().Uuid;
                    String file = FileBinaryConvertHelper.PathToString(path);
                    SQLmtm.DoInsert("t_cad_files", new string[] { "ID", "CAD_FILE_NAME", "CAD_FILE", "MD5", "ENABLE_FLAG", "UPDATE_USER", "STYLE_FIT_CD" }, new string[] { uuid, fileName, file, "", "1", "1", STYLE_FIT_CD });
                }
                SQLmtm.DoInsert("t_cad_index", new string[] { "PID", "FILE_ID", "NAME", "FILE_FLAG" }, new string[] { pid, uuid, fileName, fileFlag });
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取文件流
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public static String GetFileDataFlow(String fileid)
        {
            String sql = "SELECT\n" +
                "	CAD_FILE \n" +
                "FROM\n" +
                "	t_cad_files \n" +
                "WHERE\n" +
                "	ID = '" + fileid + "'";
            try
            {
                return SQLmtm.GetDataRow(sql)["CAD_FILE"].ToString();
            }
            catch
            {
                MessageBox.Show("未找到文件");
                return "";
            }
        }
        public static DataTable 标准版检索ByFeature(Dictionary<String, String> features)
        {
            String sql = "SELECT\n" +
                "	ID,\n" +
                "	CAD_FILE_NAME,\n" +
                "	CAD_FILE,\n" +
                "	MD5,\n" +
                "	ENABLE_FLAG,\n" +
                "	UPDATE_USER,\n" +
                "	UPDATE_DATE,\n" +
                "	STYLE_FIT_CD \n" +
                "FROM\n" +
                "	t_cad_files \n" +
                "WHERE\n" +
                "	STYLE_FIT_CD IN (\n" +
                "	SELECT DISTINCT\n" +
                "		STYLE_FIT_CD \n" +
                "	FROM\n" +
                "		t_style_fit_cd_label \n" +
                "	WHERE\n" +
                "		1 = 0 \n";
            foreach (KeyValuePair<string, string> kv in features)
            {
                sql += "OR ( LABLE_TYPE = '" + kv.Key + "' AND LABEL_ID = '" + kv.Value + "' ) \n";
            }
            sql += ");";
            return SQLmtm.GetDataTable(sql);
        }
        /// <summary>
        /// 上传标准CAD
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <param name="STYLE_FIT_CD"></param>
        /// <param name="styleid"></param>
        /// <param name="SIZE_CD"></param>
        /// <returns></returns>
        public static Boolean UploadFileStandard(String path,String fileName,String STYLE_FIT_CD,String styleid,String SIZE_CD)
        {
            String uuid = "";
            try
            {
                uuid = new FunctionHelper().Uuid;
                String file = FileBinaryConvertHelper.PathToString(path);
                SQLmtm.DoInsert("t_cad_files", new string[] { "ID", "CAD_FILE_NAME", "CAD_FILE", "MD5", "ENABLE_FLAG", "UPDATE_USER", "STYLE_FIT_CD" }, new string[] { uuid, fileName, file, "", "1", "1",STYLE_FIT_CD });
                String sql = "SELECT\n" +
                    "	* \n" +
                    "FROM\n" +
                    "	t_style_fit_cd_label \n" +
                    "WHERE\n" +
                    "	LABLE_TYPE = 'STYLE_' \n" +
                    "	AND LABEL_ID = '"+styleid+"'";
                if (SQLmtm.GetDataRow(sql)==null)
                {
                    SQLmtm.DoInsert("t_style_fit_cd_label", new string[] { "LABEL_ID", "STYLE_FIT_CD", "LABLE_TYPE" }, new string[] { styleid, STYLE_FIT_CD, "STYLE_" });
                }
                SQLmtm.DoInsert("t_style_fit_cd_label", new string[] { "LABEL_ID", "STYLE_FIT_CD", "LABLE_TYPE" }, new string[] { SIZE_CD, STYLE_FIT_CD, "FIT_" });
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
